package com.heima.media.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.common.dto.PageResponseResult;
import com.heima.common.dto.ResponseResult;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.media.dto.SensitiveDto;
import com.heima.media.entity.WmSensitive;
import com.heima.media.mapper.WmSensitiveMapper;
import com.heima.media.service.IWmSensitiveService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.HashSet;
import java.util.List;

/**
 * <p>
 * 敏感词信息表 服务实现类
 * </p>
 *
 * @author sly
 * @since 2022-03-14
 */
@Service
public class WmSensitiveServiceImpl extends ServiceImpl<WmSensitiveMapper, WmSensitive> implements IWmSensitiveService {

    @Override
    public ResponseResult listByName(SensitiveDto dto) {
        //非空判断
        if (dto == null || StringUtils.isEmpty(dto)) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        LambdaQueryWrapper<WmSensitive> query = new LambdaQueryWrapper<>();
        //有名字，按照名字模糊查询
        if (dto.getName() != null || !StringUtils.isEmpty(dto.getName())) {
            query.like(WmSensitive::getSensitives, dto.getName());
        }
        //按照时间倒排
        query.orderByDesc(WmSensitive::getCreatedTime);
        //构建分页对象
        IPage<WmSensitive> ipage = new Page<>(dto.getPage(), dto.getSize());
        //分页查询
        ipage = this.page(ipage, query);
        //返回结果
        return new PageResponseResult(dto.getPage(), dto.getSize(), ipage.getTotal(), ipage.getRecords());
    }

    @Override
    public ResponseResult updateSensitive(WmSensitive param) {
        //构建查询条件
        LambdaQueryWrapper<WmSensitive> wrapper = new LambdaQueryWrapper<>();
        List<WmSensitive> sensitiveList = this.baseMapper.selectList(wrapper);

        HashSet<String> sensitiveSets = new HashSet<>();
        WmSensitive wmSensitive1 = this.getById(param.getId());
        //获取参数
        String sensitive = param.getSensitives();
        //遍历
      for (WmSensitive wmSensitive : sensitiveList) {
            //获取数据库中的敏感词
            String sensitives = wmSensitive.getSensitives();
            //判断敏感词是否相同
            if (sensitive.equals(sensitives)) {
                //相同 提示数据已存在
               return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
            }

        }

        //不相同更新信息
        wmSensitive1.setSensitives(sensitive);
        wmSensitive1.setCreatedTime(new Date());
        this.updateById(wmSensitive1);
        return ResponseResult.okResult();
    }




    @Autowired
    private WmSensitiveMapper wmSensitiveMapper;
    @Override
    public ResponseResult deleteId(Integer id) {
        if (!StringUtils.isEmpty(id)) {
            LambdaQueryWrapper<WmSensitive> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(WmSensitive::getId, id);
            wmSensitiveMapper.delete(queryWrapper);
            return ResponseResult.okResult();
        }
        else {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
    }

    @Override
    public ResponseResult saveWmSensitive(WmSensitive entity) {
        // 校验参数是否为空
        if (entity == null || StringUtils.isEmpty(entity.getSensitives())) {
            // 返回一个错误提示
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        // 保存新增的敏感词
        // 判断敏感词是否已经存在数据库中
        // 根据敏感词查询
        LambdaQueryWrapper<WmSensitive> query = new LambdaQueryWrapper<>();
        query.eq(WmSensitive::getSensitives, entity.getSensitives());
        int count = this.count(query);
        // 已经存在不能新增,给前端一个友好提示
        if (count > 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
        }
        // 不存在,新增
        // 创建时间由后台生成
        entity.setCreatedTime(new Date());
        this.save(entity);
        return ResponseResult.okResult();
    }
}
